package org.blogwu.learn.netty学习.chapter0.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {

    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1", 9999);
            OutputStream outputStream = socket.getOutputStream();
            InputStream inputStream = socket.getInputStream();
            outputStream.write("服务端是个大傻逼".getBytes());
            outputStream.flush();
            byte[] buffer = new byte[255];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                //输出获取到所有字节  16进制
                System.out.println(bytesToHexString(buffer));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
}
